const app = getApp();
var util = require('../../utils/util.js');
var amapFile = require('../../libs/amap-wx.js');
Page({
    data: {
        service: [],
        selectUser: {},
        equipmentId: ''
    },
    onLoad: function (option) {
        this.setData({
            equipmentId: option.id
        })
        this.getService(option.id)

    },
    onShow: function () {
        console.log(this.data.selectUser)
        if(this.data.selectUser.id){
            this.addService(this.data.selectUser.id);
        }


    },
    getService: function (id) {
        wx.showLoading({
            title: '加载中'
        })
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/getOperator',
            method: 'GET',
            data: {
                token: app.globalData.token,
                equipment_id: id,
                type: 2
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading()
                console.log(res)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        service: res.data.operators
                    })


                }
            }
        })
    },
    addService: function (id) {
        wx.showLoading({
            title: '加载中'
        })
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/addOperator',
            method: 'GET',
            data: {
                token: app.globalData.token,
                equipment_id: that.data.equipmentId,
                user:id,
                type: 2
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading()
                console.log(res)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 3000
                    });
                    var newUser = that.data.service;
                    newUser.push(that.data.selectUser);
                    console.log(newUser)

                    that.setData({
                        service: newUser
                    })
                }
            }
        })
    },
    deleteService: function (e) {
        var id = e.target.dataset.id;
        var index =e.target.dataset.name;
        wx.showLoading({
            title: '加载中'
        })
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/delOperator',
            method: 'GET',
            data: {
                token: app.globalData.token,
                equipment_id: that.data.equipmentId,
                id: id
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading()
                console.log(res)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '已删除',
                        icon: 'success',
                        duration: 3000
                    });

                    that.getService(that.data.equipmentId)

                }
            }
        })
    },

    selectArea: function (e) {
        wx.showLoading({
            title: '加载中'
        })
        var areaId = e.target.dataset.id;
        var areaName = e.target.dataset.name;
        var equipmentId = this.data.equipmentId;
        console.log(areaName)

        var that = this
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/active/saveEquipArea',
            method: 'GET',
            data: {
                token: app.globalData.token,
                equipment_id: equipmentId,
                equipment_area: areaId
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    console.log(areaName)
                    prevPage.setData({
                        selectedArea: areaName
                    });
                    wx.navigateBack({
                        delta: 1
                    })
                }
            }
        })
    },
    chooseService: function () {
        wx.navigateTo({
            url: '/pages/choose-user-service/choose-user-service'
        })
    }


})